package com.training.imenu.impl.chengshu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.training.imenu.chengshu.IMenu;
import com.training.pojo.chengshu.FunUser;
import com.training.pojo.chengshu.UFunction;
import com.training.pojo.chengshu.UserRole;

public class MenuImpl extends HibernateDaoSupport implements IMenu {

	// 查询主模块
	@SuppressWarnings("unchecked")
	public List<UFunction> queryModule(Integer uid) {
		HibernateTemplate htp = this.getHibernateTemplate();

		// 根据用户id搜索出对应的角色
		String ursql = "from UserRole as ur where ur.UUser.uid = ?";
		List<UserRole> urlist = htp.find(ursql, uid);

		// 根据角色id搜索出对应的功能
		List<FunUser> fulist = new ArrayList<FunUser>();
		String fusql = "from FunUser as fu where fu.URole.roleid = ?";
		for (UserRole ur : urlist) {
			List<FunUser> f = htp.find(fusql, ur.getURole().getRoleid());
			fulist.addAll(f);
		}

		// 根据功能id搜索出模块信息
		List<UFunction> uflist = new ArrayList<UFunction>();
		String ufsql = "from UFunction as uf where uf.fid = (select modeid from UFunction as uft where uft.fid = ?)";
		for (FunUser fuser : fulist) {
			List<UFunction> u = htp.find(ufsql, fuser.getUFunction().getFid());
			uflist.addAll(u);
		}
		return uflist;
	}

	// 联立主模块及对应子功能
	@SuppressWarnings("unchecked")
	public Map<String, List<UFunction>> menuList(List<UFunction> ulist,
			Integer uid) {

		Map<String, List<UFunction>> menumap = new HashMap<String, List<UFunction>>();
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = "select URole.roleid from UserRole as ur where ur.UUser.uid = ?";
		List<Integer> list = htp.find(hql, uid);

		List<FunUser> uflist = new ArrayList<FunUser>();
		for (Integer i : list) {
			// System.out.println(i);
			hql = "from FunUser as fu where fu.URole.roleid = ? ";
			uflist.addAll(htp.find(hql, i));
		}

		for (UFunction uf : ulist) {
			hql = "from UFunction as uf where uf.modeid = ? and uf.fid = ?";
			List<UFunction> slist = new ArrayList<UFunction>();
			for (FunUser i : uflist) {
				slist.addAll(htp.find(hql, uf.getFid(), i.getUFunction()
						.getFid()));
			}
			menumap.put(uf.getFname(), slist);
		}
		return menumap;
	}
	
	//移除重复项
	public List<UFunction> removemenu(List<UFunction> list){
		Integer k = 0;
		Integer k2 = 0;
		Integer k7 = 0;
		Integer k13 = 0;
		Integer k18 = 0;
		Integer k19 = 0;
		Integer listkeyleng = 0;
		Integer[] listkey = new Integer[list.size()] ;
		for(UFunction um:list){
			switch (um.getFid()) {
			case 2:
				if(k2>0){
					listkey[listkeyleng] = k;
					listkeyleng++;
				}else{
					k2 = 1;
				}
				break;
			case 7:
				if(k7>0){
					listkey[listkeyleng] = k;
					listkeyleng++;
				}else{
					k7 = 1;
				}
				break;
			case 13:
				if(k13>0){
					listkey[listkeyleng] = k;
					listkeyleng++;
				}else{
					k13 = 1;
				}
				break;
			case 18:
				if(k18>0){
					listkey[listkeyleng] = k;
					listkeyleng++;
				}else{
					k18 = 1;
				}
				break;
			case 19:
				if(k19>0){
					listkey[listkeyleng] = k;
					listkeyleng++;
				}else{
					k19 = 1;
				}
				break;
			default:
				break;
			}
			k++;
		}
		for(int i = listkeyleng-1;i>=0;i--){
			int m = listkey[i];
			list.remove(m);
		}
		return list;
	}

	public static void main(String[] args) {
		UFunction u = new UFunction();
		u.setFname("机房管理");
		u.setFid(2);
		List<UFunction> ulist = new ArrayList<UFunction>();
		ulist.add(u);
		ApplicationContext act = new ClassPathXmlApplicationContext(
				"file:E:/myeclipse/Workspaces/training/WebRoot/WEB-INF/applicationContext.xml");
		MenuImpl me = (MenuImpl) act.getBean("menuimpl");
		List<UFunction> list = me.queryModule(2);
		list = me.removemenu(list);
		
		
	}
}
